<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_mail=new mail();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["id"])){$_mail->set_id($_POST["id"]);}
		if(isset($_POST["sujet"])){$_mail->set_sujet($_POST["sujet"]);}
		if(isset($_POST["mail"])){$_mail->set_mail($_POST["mail"]);}
		if(isset($_POST["descritpion"])){$_mail->set_descritpion($_POST["descritpion"]);}
		if(isset($_POST["id_phase_desc"])){$_mail->set_id_phase_desc($_POST["id_phase_desc"]);}
		$res=$_mail->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["id"])){
		$_mail->set_id($_POST["id"]);
		$res=$_mail->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["id"])){$_mail->set_id($_POST["id"]);}
		if(isset($_POST["sujet"])){$_mail->set_sujet($_POST["sujet"]);}
		if(isset($_POST["mail"])){$_mail->set_mail($_POST["mail"]);}
		if(isset($_POST["descritpion"])){$_mail->set_descritpion($_POST["descritpion"]);}
		if(isset($_POST["id_phase_desc"])){$_mail->set_id_phase_desc($_POST["id_phase_desc"]);}
		$res=$_mail->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["id"]) && !empty($_POST["id"])){
		$constraint.=" AND id='".$_POST["id"]."'";
	}

	if(isset($_POST["sujet"]) && !empty($_POST["sujet"])){
		$constraint.=" AND sujet='".$_POST["sujet"]."'";
	}

	if(isset($_POST["mail"]) && !empty($_POST["mail"])){
		$constraint.=" AND mail='".$_POST["mail"]."'";
	}

	if(isset($_POST["descritpion"]) && !empty($_POST["descritpion"])){
		$constraint.=" AND descritpion='".$_POST["descritpion"]."'";
	}

	if(isset($_POST["id_phase_desc"]) && !empty($_POST["id_phase_desc"])){
		$constraint.=" AND id_phase_desc='".$_POST["id_phase_desc"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_mail->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_mail->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>